package hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class ScanTest {

	public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "master");
		
		HBaseAdmin admin = new HBaseAdmin(conf);
		HTable table = new HTable(conf, "user");
		try {
			
			Scan scan = new Scan();
			
			//默认缓存大小为1，设置成一个合理的值，可以减少scan过程中next()的时间开销，代价是客户端的内存  
			scan.setCaching(1000);
			scan.setCacheBlocks(false);
			
			ResultScanner results = table.getScanner(scan);
			for(Result result : results){
				
				System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes("teacher"), Bytes.toBytes("math"))));
				System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes("teacher"), Bytes.toBytes("chinese"))));
				System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes("teacher"), Bytes.toBytes("english"))));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			table.close();
			admin.close();
		}
	}
}
